//
//  YHMyMachineScreenMenuView.m
//  YHApp
//
//  Created by -- on 2021/12/19.
//

#import "HAMyMachineScreenPopView.h"
#import "HAChooseTabPopView.h"
#import "HAPolicyListMo.h"

@interface HAMyMachineScreenPopView ()<UITextFieldDelegate>

/***/
@property (nonatomic, strong) UITextField *seachField;
/***/
@property (nonatomic, strong) NSMutableArray *buttonArray;
/***/
@property (nonatomic, strong) NSArray *itemArray;
/***/
@property (nonatomic, assign) NSInteger selStateIndex;
/***/
@property (nonatomic, strong) UIButton *mybutton;
/***/
@property (nonatomic, strong) UIButton *tebutton;
/***/
@property (nonatomic, strong) UILabel *policyConLabel;
/***/
@property (nonatomic, strong) UILabel *canalItemConLabel;

@property (nonatomic, strong) UILabel *canalConLabel;


@end

@implementation HAMyMachineScreenPopView

-(instancetype)init {
    if (self = [super init]) {

//        0=>未绑定，1=》已绑定，2=>已激活，3=>伪激活 4=>已达标
//        self.itemArray = [NSArray arrayWithObjects:@"未绑定",@"已绑定",@"已激活",@"伪激活",@"已达标", nil];
        self.itemArray = [NSArray arrayWithObjects:@"未绑定",@"已绑定",@"未激活",@"伪激活",@"已激活",@"已达标",@"未达标", nil];
        self.selStateIndex = -1;
    }
    return self;
    
}

-(void)seachBtnClick {
    [self sureButtonClick];
}

-(void)mybuttonClick {
    self.isTeam = @"0";
}

-(void)tebuttonClick {
    self.isTeam = @"1";
}

-(void)buttonClick:(UIButton *)button {
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        button.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    }
    [button setTitleColor:[UIColor colorWithHexString:@"#FFC4AC"] forState:UIControlStateNormal];
    button.layer.borderColor = [UIColor colorWithHexString:@"#FFC4AC"].CGColor;
    
    self.selStateIndex = button.tag;
}

-(void)termmodViewClick {
    NSMutableArray *dicts = [NSMutableArray array];
    for (HACanalItemMo *mo in self.canalItemMoArr) {
        [dicts addObject:@{@"color":@"#373737",@"title":mo.text?:@""}];
    }
    HAChooseTabPopView *menu = [[HAChooseTabPopView alloc]initWithDicts:dicts title:@"选择政策"];
    [menu show];
    menu.YHChooseTabMenuCellBlock = ^(NSString * _Nonnull title) {
        for (HACanalItemMo *mo in self.canalItemMoArr) {
            if ([title isEqualToString:mo.text]) {
                mo.isSel = YES;
                self.policyConLabel.text = mo.text;
            }else {
                mo.isSel = NO;
            }
        }
    };
}

-(void)canalViewClick {
    NSMutableArray *dicts = [NSMutableArray array];
    for (HACanalItemMo *mo in [HASingle single].canalItemArray) {
        [dicts addObject:@{@"color":@"#373737",@"title":mo.text?:@""}];
    }
    HAChooseTabPopView *menu = [[HAChooseTabPopView alloc]initWithDicts:dicts title:@"选择产品通道"];
    [menu show];
    menu.YHChooseTabMenuCellBlock = ^(NSString * _Nonnull title) {
        for (HACanalItemMo *mo in [HASingle single].canalItemArray) {
            if ([title isEqualToString:mo.text]) {
                mo.isSel = YES;
                self.canalConLabel.text = mo.text;
            }else {
                mo.isSel = NO;
            }
        }
    };
}


-(void)policyViewClick {

    NSMutableArray *dicts = [NSMutableArray array];
    for (HAPolicyListMo *mo in self.policyLists) {
        [dicts addObject:@{@"color":@"#373737",@"title":mo.name?:@""}];
    }
    HAChooseTabPopView *menu = [[HAChooseTabPopView alloc]initWithDicts:dicts title:@"选择政策"];
    [menu show];
    menu.YHChooseTabMenuCellBlock = ^(NSString * _Nonnull title) {
        for (HAPolicyListMo *mo in self.policyLists) {
            if ([title isEqualToString:mo.name]) {
                mo.isSel = YES;
                self.policyConLabel.text = mo.name;
            }else {
                mo.isSel = NO;
            }
        }
    };

}

-(void)resetButtonClick {
    self.seachField.text = @"";
    
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        button.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    }
    
    for (HACanalItemMo *mo in self.canalItemMoArr) {
        mo.isSel = NO;
    }
    self.canalItemConLabel.text = @"请选择产品型号";
    
    for (HAPolicyListMo *mo in self.policyLists) {
        mo.isSel = NO;
    }
    self.policyConLabel.text = @"请选择政策";
    
    self.isTeam = @"";
    
    self.selStateIndex = -1;
}

-(void)sureButtonClick {
    [self dismiss];
    
    NSString *searchText = self.seachField.text ?: @"";
    NSString *state = @"";
    if (self.selStateIndex != -1) {
        state = [NSString stringWithFormat:@"%ld",(long)self.selStateIndex];
    }
    
    if (self.HAMyMachineScreenPopViewBlock) {
        self.HAMyMachineScreenPopViewBlock(searchText, state, self.isTeam);
    }
}

-(void)setKeyword:(NSString *)keyword {
    _keyword = keyword;
    self.seachField.text = keyword;
}

-(void)setState:(NSString *)state {
    _state = state;
    if ([NSString isEmptyString:state]) {
        return;
    }
    self.selStateIndex = [state integerValue];
    NSString *stateText = @"";
//    if (state.intValue == 0) {
//        stateText = @"未绑定";
//    }
//    if (state.intValue == 1) {
//        stateText = @"已绑定";
//    }
//    if (state.intValue == 2) {
//        stateText = @"已激活";
//    }
//    if (state.intValue == 20) {
//        stateText = @"伪激活";
//    }
//    if (state.intValue == 3) {
//        stateText = @"已达标";
//    }
    
    stateText = self.itemArray[self.selStateIndex];
    for (UIButton *button in self.buttonArray) {
        if ([button.currentTitle isEqualToString:stateText]) {
            [button setTitleColor:[UIColor colorWithHexString:@"#FFC4AC"] forState:UIControlStateNormal];
            button.layer.borderColor = [UIColor colorWithHexString:@"#FFC4AC"].CGColor;
        }
    }
    
}

-(void)setIsTeam:(NSString *)isTeam {
    _isTeam = isTeam;
    
    if ([isTeam isEqualToString:@"0"]) {
        [self.mybutton setTitleColor:[UIColor colorWithHexString:@"#FFC4AC"] forState:UIControlStateNormal];
        self.mybutton.layer.borderColor = [UIColor colorWithHexString:@"#FFC4AC"].CGColor;
        
        [self.tebutton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        self.tebutton.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    }
    if ([isTeam isEqualToString:@"1"]) {
        [self.tebutton setTitleColor:[UIColor colorWithHexString:@"#FFC4AC"] forState:UIControlStateNormal];
        self.tebutton.layer.borderColor = [UIColor colorWithHexString:@"#FFC4AC"].CGColor;
        
        [self.mybutton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        self.mybutton.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    }
    
}

-(void)loadViews {
    
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
    
    CGFloat alertViewW = kUIScreen_w*0.85;
    CGFloat alertViewH = kUIScreen_h;
    CGFloat alertViewX = kUIScreen_w-alertViewW;
    CGFloat alertViewY = kUIScreen_h-alertViewH;
    self.alertView.frame = CGRectMake(alertViewX, alertViewY, alertViewW, alertViewH);
    [self addSubview: self.alertView];
    self.alertView.backgroundColor = [UIColor whiteColor];
    self.alertView.layer.cornerRadius = 0;
    self.alertView.layer.masksToBounds = YES;
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#333333" text:@"全部筛选" alignment:0];
    [self.alertView addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44+20);
        make.left.mas_equalTo(20);
    }];
    
    UIView *lineView = [UIView ha_createView];
    lineView.backgroundColor = [UIColor khaBGColor];
    [self.alertView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(titLabel.mas_bottom).offset(50);
        make.height.mas_offset(1);
    }];
    
    UIButton *seachBtn = [[UIButton alloc]init];
    [seachBtn setImage:[UIImage xl_cuttingImageWith:[UIImage imageNamed:@"yh_10"] toSize:CGSizeMake(22, 22)] forState:UIControlStateNormal];
    [seachBtn addTarget:self action:@selector(seachBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:seachBtn];
    [seachBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(lineView);
        make.bottom.mas_equalTo(lineView).offset(-4);
        make.width.mas_offset(32);
        make.height.mas_offset(32);
    }];
    
    UITextField *seachField = [[UITextField alloc]init];
    self.seachField = seachField;
    seachField.delegate = self;
    seachField.placeholder = @"请输入机具编号";
    seachField.font = [UIFont systemFontOfSize:18];
    [self.alertView addSubview:seachField];
    [seachField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(lineView);
        make.right.equalTo(seachBtn.mas_left).offset(-5);
        make.centerY.mas_equalTo(seachBtn);
        make.height.equalTo(@32);
    }];
    
    UIButton *mybutton = [[UIButton alloc]init];
    self.mybutton = mybutton;
    [mybutton setTitle:@"我的机具" forState:UIControlStateNormal];
    [mybutton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    mybutton.titleLabel.font = [UIFont systemFontOfSize:14];
    mybutton.layer.cornerRadius = 8;
    mybutton.layer.masksToBounds = YES;
    mybutton.layer.borderWidth = 1;
    mybutton.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    [mybutton addTarget:self action:@selector(mybuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:mybutton];
    [mybutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(24);
        make.top.mas_equalTo(lineView.mas_bottom).offset(18);
        make.height.mas_offset(24);
        make.width.mas_offset(72);
    }];
        
    UIButton *tebutton = [[UIButton alloc]init];
    self.tebutton = tebutton;
    [tebutton setTitle:@"团队机具" forState:UIControlStateNormal];
    [tebutton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    tebutton.titleLabel.font = [UIFont systemFontOfSize:14];
    tebutton.layer.cornerRadius = 8;
    tebutton.layer.masksToBounds = YES;
    tebutton.layer.borderWidth = 1;
    tebutton.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    [tebutton addTarget:self action:@selector(tebuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:tebutton];
    [tebutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(mybutton.mas_right).offset(15);
        make.top.mas_equalTo(lineView.mas_bottom).offset(18);
        make.height.mas_offset(24);
        make.width.mas_offset(72);
    }];
    
    UILabel *tit2Label = [UILabel ha_NLabelWithFont:18 textColor:@"#333333" text:@"机具状态" alignment:0];
    [self.alertView addSubview:tit2Label];
    [tit2Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineView.mas_bottom).offset(18+60);
        make.left.mas_equalTo(20);
    }];
    
    CGFloat btnY = app_topH+44+20+128+60;
    CGFloat btnX = 20;
    CGFloat btnH = 24;
    CGFloat space = 10;
    
    [self.buttonArray removeAllObjects];
    
    for (int i = 0; i < self.itemArray.count; i ++) {
        
        UIButton *button = [[UIButton alloc]init];
        [button setTitle:self.itemArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag= i;
        button.layer.cornerRadius = 8;
        button.layer.masksToBounds = YES;
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.alertView addSubview:button];
        
        CGFloat btnW = [self.itemArray[i] sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}].width+24;
        
        if (btnX + btnW + space > alertViewW-12) {
            btnY = btnY + btnH + 10;
            btnX = 20;
        }
        button.frame = CGRectMake(btnX, btnY, btnW, btnH);
        btnX = btnX + btnW + space;//下一个x
        
        [self.buttonArray addObject:button];
    }
    
    
    if ([HASingle single].canalItemArray.count > 0) {
        btnY += 48;
        UIView *itemView = [[UIView alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(canalViewClick)];
        [itemView addGestureRecognizer:tap];
        [self.alertView addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-5);
            make.top.mas_equalTo(btnY);
            make.height.mas_offset(44);
        }];
        
        UIView *itemlineView = [UIView ha_createView];
        itemlineView.backgroundColor = [UIColor khaBGColor];
        [itemView addSubview:itemlineView];
        [itemlineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.height.mas_offset(1);
        }];
        
        UIImageView *itemRImgV = [UIImageView ha_createImageView];
        itemRImgV.image = [UIImage imageNamed:@"ic_cell_more"];
        [itemView addSubview:itemRImgV];
        [itemRImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(5);
            make.height.mas_offset(10);
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-20);
        }];
        
        UILabel *itemtitLabel = [UILabel ha_NLabelWithFont:14 textColor:@"#333333" text:@"产品通道" alignment:0];
        [itemView addSubview:itemtitLabel];
        [itemtitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.width.mas_offset(72);
        }];
        
        UILabel *itemConLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#666666" text:@"请选择产品通道" alignment:2];
        self.canalConLabel = itemConLabel;
        [itemView addSubview:itemConLabel];
        [itemConLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(itemRImgV.mas_left).offset(-5);
            make.left.equalTo(itemtitLabel.mas_right);
        }];
        
        for (HACanalItemMo *mo in [HASingle single].canalItemArray) {
            if (mo.isSel) {
                self.canalConLabel.text = mo.text;
                break;
            }
        }
    }

    
    if (self.canalItemMoArr.count > 0) {
        btnY += 48;
        UIView *itemView = [[UIView alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(termmodViewClick)];
        [itemView addGestureRecognizer:tap];
        [self.alertView addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-5);
            make.top.mas_equalTo(btnY);
            make.height.mas_offset(44);
        }];
        
        UIView *itemlineView = [UIView ha_createView];
        itemlineView.backgroundColor = [UIColor khaBGColor];
        [itemView addSubview:itemlineView];
        [itemlineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.height.mas_offset(1);
        }];
        
        UIImageView *itemRImgV = [UIImageView ha_createImageView];
        itemRImgV.image = [UIImage imageNamed:@"ic_cell_more"];
        [itemView addSubview:itemRImgV];
        [itemRImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(5);
            make.height.mas_offset(10);
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-20);
        }];
        
        UILabel *itemtitLabel = [UILabel ha_NLabelWithFont:14 textColor:@"#333333" text:@"产品型号" alignment:0];
        [itemView addSubview:itemtitLabel];
        [itemtitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.width.mas_offset(72);
        }];
        
        UILabel *itemConLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#666666" text:@"请选择产品型号" alignment:2];
        self.canalItemConLabel = itemConLabel;
        [itemView addSubview:itemConLabel];
        [itemConLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(itemRImgV.mas_left).offset(-5);
            make.left.equalTo(itemtitLabel.mas_right);
        }];
        
        for (HACanalItemMo *mo in self.canalItemMoArr) {
            if (mo.isSel) {
                self.canalItemConLabel.text = mo.text;
                break;
            }
        }
    }

    
    if (self.policyLists.count > 0) {
        btnY += 48;
        UIView *policyView = [[UIView alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(policyViewClick)];
        [policyView addGestureRecognizer:tap];
        [self.alertView addSubview:policyView];
        [policyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.top.mas_equalTo(btnY);
            make.height.mas_offset(44);
        }];
        
        UIView *policylineView = [UIView ha_createView];
        policylineView.backgroundColor = [UIColor khaBGColor];
        [policyView addSubview:policylineView];
        [policylineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.height.mas_offset(1);
        }];

        UIImageView *policyRImgV = [UIImageView ha_createImageView];
        policyRImgV.image = [UIImage imageNamed:@"ic_cell_more"];
        [policyView addSubview:policyRImgV];
        [policyRImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(5);
            make.height.mas_offset(10);
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-20);
        }];

        UILabel *policytitLabel = [UILabel ha_NLabelWithFont:14 textColor:@"#333333" text:@"选择政策" alignment:0];
        [policyView addSubview:policytitLabel];
        [policytitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.width.mas_offset(72);
        }];

        UILabel *policyConLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#666666" text:@"请选择政策" alignment:2];
        self.policyConLabel = policyConLabel;
        [policyView addSubview:policyConLabel];
        [policyConLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(policyRImgV.mas_left).offset(-5);
            make.left.equalTo(policytitLabel.mas_right);
        }];
        
        for (HAPolicyListMo *mo in self.policyLists) {
            if (mo.isSel) {
                self.policyConLabel.text = mo.name;
                break;
            }
        }
    }
    
    UIButton *resetButton = [[UIButton alloc]init];
    [resetButton setTitle:@"重置" forState:UIControlStateNormal];
    [resetButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    resetButton.titleLabel.font = [UIFont systemFontOfSize:18];
    resetButton.layer.cornerRadius = 2;
    resetButton.layer.masksToBounds = YES;
    resetButton.layer.borderWidth = 1;
    resetButton.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    [resetButton addTarget:self action:@selector(resetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:resetButton];
    [resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(44);
        make.width.mas_offset(120);
        make.top.mas_equalTo(btnY+40+80+20);
        make.centerX.mas_equalTo(-70);
    }];
    
    UIButton *sureButton = [[UIButton alloc]init];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:18];
    sureButton.layer.cornerRadius = 2;
    sureButton.layer.masksToBounds = YES;
    sureButton.layer.borderWidth = 1;
    sureButton.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    sureButton.backgroundColor = [UIColor colorWithHexString:@"#333333"];
    [sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(44);
        make.width.mas_offset(120);
        make.top.mas_equalTo(btnY+40+80+20);
        make.centerX.mas_equalTo(70);
    }];


    self.isTeam = self.isTeam;
    self.state = self.state;
    self.seachField.text = self.keyword;
}


- (NSMutableArray *)buttonArray
{
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

- (NSMutableArray *)canalItemMoArr
{
    if (!_canalItemMoArr) {
        _canalItemMoArr = [NSMutableArray array];
        
    }
    return _canalItemMoArr;
}

-(void)show {
    
    UIWindow *keyWindow =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    
    [keyWindow addSubview:self];
    CGRect rect = self.alertView.frame;
    rect.origin.x = kUIScreen_w;
    self.alertView.frame = rect;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.x = kUIScreen_w-kUIScreen_w*0.85;
        self.alertView.frame = rect;
    }];
}
-(void)dismiss {
    [UIView animateWithDuration:0.25 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.x = kUIScreen_w;
        self.alertView.frame = rect;
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        [self removeFromSuperview];
    }];
}

@end
